//
//  PShareController.swift
//  CJPack
//
//  Created by chenjingwei on 2025/4/28.
//

import UIKit
import Social
import Accounts
import SafariServices

class PShareController: CJBaseBackController, UIDocumentInteractionControllerDelegate {
    
    let text = "你的文本你的文本你的文本你的文本你的文本你的文本"
    let image = UIImage(named: "photo_0")
    

    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
    }
    
    func setupView() {
        
        let twB1 = UIButton(type: .custom)
        twB1.setTitle("推特分享 Scheme", for: .normal)
        twB1.setTitleColor(.black, for: .normal)
        twB1.frame = CGRect(x: 20, y: 100, width: 200, height: 30)
        view.addSubview(twB1)
        twB1.addTarget(self, action: #selector(twB1Action), for: .touchUpInside)
        
        let twB2 = UIButton(type: .custom)
        twB2.setTitle("推特分享 Safari", for: .normal)
        twB2.setTitleColor(.black, for: .normal)
        twB2.frame = CGRect(x: 20, y: twB1.cj_bottom + 10, width: 200, height: 30)
        view.addSubview(twB2)
        twB2.addTarget(self, action: #selector(twB2Action), for: .touchUpInside)
        
        let twB3 = UIButton(type: .custom)
        twB3.setTitle("推特分享 Compose", for: .normal)
        twB3.setTitleColor(.black, for: .normal)
        twB3.frame = CGRect(x: 20, y: twB2.cj_bottom + 10, width: 200, height: 30)
        view.addSubview(twB3)
        twB3.addTarget(self, action: #selector(twB3Action), for: .touchUpInside)
        
        let twB4 = UIButton(type: .custom)
        twB4.setTitle("分享 Activity", for: .normal)
        twB4.setTitleColor(.black, for: .normal)
        twB4.frame = CGRect(x: 20, y: twB3.cj_bottom + 10, width: 200, height: 30)
        view.addSubview(twB4)
        twB4.addTarget(self, action: #selector(twB4Action), for: .touchUpInside)
        
        let insB1 = UIButton(type: .custom)
        insB1.setTitle("ins分享 Scheme", for: .normal)
        insB1.setTitleColor(.black, for: .normal)
        insB1.frame = CGRect(x: 20, y: twB4.cj_bottom + 10, width: 200, height: 30)
        view.addSubview(insB1)
        insB1.addTarget(self, action: #selector(insB1Action), for: .touchUpInside)
        
        let fbB1 = UIButton(type: .custom)
        fbB1.setTitle("fb分享 Compose", for: .normal)
        fbB1.setTitleColor(.black, for: .normal)
        fbB1.frame = CGRect(x: 20, y: insB1.cj_bottom + 10, width: 200, height: 30)
        view.addSubview(fbB1)
        fbB1.addTarget(self, action: #selector(fbB1Action), for: .touchUpInside)
    }
    
}

extension PShareController {
    
    @objc func twB1Action() {
        guard let et = text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
            return
        }
        let urlString = "twitter://post?message=\(et)"
        let url = URL(string: urlString)!
        if UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            let webURLString = "https://twitter.com/intent/tweet?text=\(text)"
            if let webURL = URL(string: webURLString) {
                UIApplication.shared.open(webURL, options: [:], completionHandler: nil)
            }
        }
    }
    
    @objc func twB2Action() {
        if let url = URL(string: "https://twitter.com/intent/tweet?text=\(text)") {
            let safariVC = SFSafariViewController(url: url)
            self.present(safariVC, animated: true, completion: nil)
        }
    }
    
    @objc func twB3Action() {
        let cvc = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
        cvc?.setInitialText(text)
        cvc?.add(image)
        cvc?.completionHandler = { result in
            
        }
        if let cvc = cvc {
            self.present(cvc, animated: true)
        }
    }
        
    @objc func twB4Action() {
        shareToTwitter(text: text, image: image, from: self)
    }
    
    func shareToTwitter(text: String, image: UIImage?, from viewController: UIViewController) {
        var items: [Any] = [text]
        if let image = image {
            items.append(image)
        }
        let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
        activityVC.excludedActivityTypes = [
                .airDrop,
                .copyToPasteboard,
                .saveToCameraRoll,
                .mail,
                .message
            ]
        viewController.present(activityVC, animated: true, completion: nil)
    }

}

extension PShareController {
    
    @objc func insB1Action() {
        guard let urlScheme = URL(string: "instagram-stories://share?source_application=com.my.app") else {
            print("URL scheme is invalid")
            return
        }
        if UIApplication.shared.canOpenURL(urlScheme) {
            guard let imageData = image!.pngData() else {
                print("Failed to get PNG data from image")
                return
            }
            let pasteboardItems: [String: Any] = [
                "com.instagram.sharedSticker.backgroundImage": imageData,
                //"com.instagram.sharedSticker.backgroundVideo": videoData
                "com.instagram.sharedSticker.contentURL": "https://yourapp.com/yourpage",
            ]
            let pasteboardOptions = [
                UIPasteboard.OptionsKey.expirationDate:Date().addingTimeInterval(300) // 5分钟
            ]
            UIPasteboard.general.setItems([pasteboardItems], options: pasteboardOptions)
            
            UIApplication.shared.open(urlScheme, options: [:], completionHandler: nil)
        } else {
            print("Instagram app is not installed")
        }
    }
    
    
}

extension PShareController {
    
    @objc func fbB1Action() {
        let cvc = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        cvc?.setInitialText(text)
        cvc?.add(image)
        cvc?.completionHandler = { result in
            
        }
        if let cvc = cvc {
            self.present(cvc, animated: true)
        }
    }
}
